#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2012 OpenWrt.org

START=60
STOP=10
USE_PROCD=1

smb_header() {
	config_get samba_iface $1 interface "loopback lan"

	# resolve interfaces
	local interfaces=$(
		. /lib/functions/network.sh

		local net
		for net in $samba_iface; do
			local device
			network_is_up $net || continue
			network_get_device device "$net"
			echo -n "${device:-$net} "
		done
	)

	local name workgroup description charset
	local hostname="$(sed 's/\..*//' /proc/sys/kernel/hostname | tr -d '{};%?=#\n')"

	config_get name $1 name "${hostname:-Lede}"
	config_get workgroup $1 workgroup "WORKGROUP"
	config_get description $1 description "Samba on ${hostname:-Lede}"
	config_get charset $1 charset "UTF-8"

	mkdir -p /var/etc
	sed -e "s#|NAME|#$name#g" \
		-e "s#|WORKGROUP|#$workgroup#g" \
		-e "s#|DESCRIPTION|#$description#g" \
		-e "s#|INTERFACES|#$interfaces#g" \
		-e "s#|CHARSET|#$charset#g" \
		/etc/samba/smb.conf.template >/var/etc/smb.conf

	local homes
	config_get_bool homes $1 homes 0
	[ $homes -gt 0 ] && {
		cat <<EOT >>/var/etc/smb.conf

[homes]
	comment     = Home Directories
	browsable   = no
	read only   = no
	create mode = 0750
EOT
	}

	[ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
}

smb_add_share() {
	local name
	local path
	local users
	local read_only
	local guest_ok
	local create_mask
	local dir_mask
	local browseable

	config_get name $1 name
	config_get path $1 path
	config_get users $1 users
	config_get read_only $1 read_only
	config_get guest_ok $1 guest_ok
	config_get create_mask $1 create_mask
	config_get dir_mask $1 dir_mask
	config_get browseable $1 browseable

	[ -z "$name" -o -z "$path" ] && return

	echo -e "\n[$name]\n\tpath = $path" >>/var/etc/smb.conf
	[ -n "$users" ] && echo -e "\tvalid users = $users" >>/var/etc/smb.conf
	[ -n "$read_only" ] && echo -e "\tread only = $read_only" >>/var/etc/smb.conf
	[ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >>/var/etc/smb.conf
	[ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >>/var/etc/smb.conf
	[ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >>/var/etc/smb.conf
	[ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >>/var/etc/smb.conf
}

get_auto_share() {
	local auto_share_value
	config_get auto_share_value "$1" auto_share 0
	[ "$auto_share_value" -eq 1 ] && auto_share=1
}

get_auto_share_ro() {
	local auto_share_ro_value
	config_get auto_share_ro_value "$1" auto_share_ro 0
	[ "$auto_share_ro_value" -eq 1 ] && auto_share_ro=1
}

# $1 read_only (0 or 1)
process_read_only_opt() {
	local perm_ro="no"
	[ "$1" -eq 1 ] && perm_ro="yes"
	sed -i "/^[[:space:]]*option[[:space:]]\+[\"\']\{0,1\}read_only[\"\']\{0,1\}[[:space:]]\+[\"\']\{0,1\}\(yes\|no\)[\"\']\{0,1\}[[:space:]]*$/ s/[\"\']\{0,1\}\(yes\|no\)[\"\']\{0,1\}[[:space:]]*$/'$perm_ro'/g" /var/run/config/samba
}

autosamba_lock() {
	mkdir -p /var/lock
	lock /var/lock/autosamba.lock
}

autosamba_unlock() {
	lock -u /var/lock/autosamba.lock
}

init_config() {
	local auto_share=0
	local auto_share_ro=0
	config_load samba
	config_foreach get_auto_share samba
	config_foreach get_auto_share_ro samba
	[ "$auto_share" -eq 1 ] && {
		autosamba_lock
		[ -f /var/run/config/samba ] || {
			mkdir -p /var/run/config && touch /var/run/config/samba
		}
		process_read_only_opt $auto_share_ro
		CONFIG_APPEND=1 UCI_CONFIG_DIR=/var/run/config config_load samba
		autosamba_unlock
	}

	config_foreach smb_header samba
	config_foreach smb_add_share sambashare
}

service_triggers() {
	procd_add_reload_trigger samba

	local i
	for i in $samba_iface; do
		procd_add_reload_interface_trigger $i
	done
}

service_running() {
	procd_running samba
}

start_service() {
	init_config

	procd_open_instance
	procd_add_mdns "smb" "tcp" "445"
	procd_set_param command /usr/sbin/smbd -F
	procd_set_param respawn
	procd_set_param file /var/etc/smb.conf
	procd_close_instance

	procd_open_instance
	procd_set_param command /usr/sbin/nmbd -F
	procd_set_param respawn
	procd_set_param file /var/etc/smb.conf
	procd_close_instance
}

reload_service() {
	stop
	start
}
